package com.fulin.lucene;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.util.Version;

public class TestLucene {
	public static void main(String[] args) {
		Version matchVersion = Version.LUCENE_6_6_0;
		Analyzer analyzer = new StandardAnalyzer(); 
		TokenStream tokenStream = analyzer.tokenStream("", new StringReader("Welcome to use lucene!")); 
		OffsetAttribute offsetAtt = tokenStream.addAttribute(OffsetAttribute.class);
		
		try {
			tokenStream.reset(); // Resets this stream to the beginning. (Required)
			while(tokenStream.incrementToken()){
				System.out.println("token: "+tokenStream.reflectAsString(true));
				System.out.println("token start offset: "+ offsetAtt.startOffset());
				System.out.println("  token end offset: "+ offsetAtt.endOffset());
			}
			tokenStream.end();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				tokenStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			analyzer.close();
		}
	}
}



















